package com.zucai365.football.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;
import com.zucai365.football.bean.CurrentFootballTeamBean;
import com.zucai365.football.bean.TeamPlayerBean;
import com.zucai365.football.service.CurrentFootballTeamService;
import com.zucai365.football.service.FootballPlayerService;
import com.zucai365.football.service.TeamPlayerService;
import com.zucai365.football.utils.Constant;
import com.zucai365.football.utils.ZuCaiUtils;

public class PlayerAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6411012366056920960L;
	
	/**
	 * 当前赛季ID
	 */
	private Long lianSaiYearId;
	
	public Long getLianSaiYearId() {
		return lianSaiYearId;
	}

	public void setLianSaiYearId( Long lianSaiYearId ) {
		this.lianSaiYearId = lianSaiYearId;
	}
	/**
	 * 联赛ID
	 */
	private Long lianSaiId;
	
	public Long getLianSaiId() {
		return lianSaiId;
	}

	public void setLianSaiId(Long lianSaiId) {
		this.lianSaiId = lianSaiId;
	}
	
	/**
	 * 球队ID
	 */
	private Long footballTeamId;
	
	public Long getFootballTeamId() {
		return footballTeamId;
	}
	
	private Long teamPlayerId;
	
	public Long getTeamPlayerId() {
		return teamPlayerId;
	}

	public void setTeamPlayerId(Long teamPlayerId) {
		this.teamPlayerId = teamPlayerId;
	}

	public void setFootballTeamId(Long footballTeamId) {
		this.footballTeamId = footballTeamId;
	}
	
	@Autowired
	@Qualifier("footballPlayerService")
	private FootballPlayerService footballPlayerService;

	public FootballPlayerService getFootballPlayerService() {
		return footballPlayerService;
	}

	public void setFootballPlayerService(FootballPlayerService footballPlayerService) {
		this.footballPlayerService = footballPlayerService;
	}
	
	@Autowired
	@Qualifier("currentFootballTeamService")
	private CurrentFootballTeamService currentFootballTeamService;
	
	public CurrentFootballTeamService getCurrentFootballTeamService() {
		return currentFootballTeamService;
	}

	public void setCurrentFootballTeamService(
			CurrentFootballTeamService currentFootballTeamService) {
		this.currentFootballTeamService = currentFootballTeamService;
	}
	
	@Autowired
	@Qualifier("teamPlayerService")
	private TeamPlayerService teamPlayerService;
	
	public TeamPlayerService getTeamPlayerService() {
		return teamPlayerService;
	}

	public void setTeamPlayerService(TeamPlayerService teamPlayerService) {
		this.teamPlayerService = teamPlayerService;
	}

	public String list(){
		
		ServletActionContext.getRequest().setAttribute( "lianSaiYearList", Constant.ALL_LIANSAI_YEAR_LIST );
		ServletActionContext.getRequest().setAttribute( "lianSaiList", Constant.ALL_LIANSAI_LIST );
		if( this.lianSaiYearId == null ){
			this.lianSaiYearId = ZuCaiUtils.CURRENT_LIANSAI_YEAR;
		}
		if( this.lianSaiId == null ){
			this.lianSaiId = ZuCaiUtils.CURRENT_LIANSAI;
		}
		
		//所有球队信息
		List<CurrentFootballTeamBean> teamList = this.currentFootballTeamService.getCurrentFootballTeamList( this.lianSaiId, this.lianSaiYearId );
		ServletActionContext.getRequest().setAttribute( "teamList", teamList );
		
		if( this.footballTeamId == null ){
			this.footballTeamId = teamList.get(0).getFootballTeam().getId();
		}
		
		//所有球员信息
		List<TeamPlayerBean> teamPlayerList = this.teamPlayerService.getTeamPlayerList(  this.lianSaiYearId,  this.lianSaiId, this.footballTeamId );
		ServletActionContext.getRequest().setAttribute( "teamPlayerList", teamPlayerList );
		
		return "list";
	}
	
	private Long currentFootballTeamId;
	
	public Long getCurrentFootballTeamId() {
		return currentFootballTeamId;
	}

	public void setCurrentFootballTeamId(Long currentFootballTeamId) {
		this.currentFootballTeamId = currentFootballTeamId;
	}
	
	/**
	 * 保存球员其它信息
	 * @return
	 */
	public String savePlayerExtend(){
		
		//下载球队球员json字符串
		CurrentFootballTeamBean currentFootballTeam = this.currentFootballTeamService.get( this.currentFootballTeamId );
		ZuCaiUtils.dowloadTeamPlayerJS( currentFootballTeam );
		
		TeamPlayerBean teamPlayer = this.teamPlayerService.get( this.teamPlayerId );
		this.footballPlayerService.updateTeamPlayer(teamPlayer);
		return "listPage";
		
	}
	
}
